package kr.co.graviti.common.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.graviti.adm.common.vo.AdmCkEditorVO;
import kr.co.graviti.common.util.FilePolicyUtil;
import kr.co.graviti.common.vo.MultipartUpLoadFileInfoVO;


import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class CkEditorImgUploadAction extends BaseFuncAction {
	
	private static final Logger _log = Logger.getLogger(CkEditorImgUploadAction.class);
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	        throws Exception {
		
		
		AdmCkEditorVO vo = (AdmCkEditorVO)form;
		FormFile file =  vo.getUpload();
		String fucNum = vo.getCKEditorFuncNum();
		HashMap filemap = new HashMap();
		boolean ckeckImgExt = true;
		
		_log.info("fucNum===>"+fucNum);
		if(file != null){
			
			ckeckImgExt = new FilePolicyUtil().checkImgExt(file.getFileName());
			
			_log.info("check==>"+ckeckImgExt);
			
			if(ckeckImgExt){
				MultipartUpLoadFileInfoVO fileInfo = 
					new FilePolicyUtil().multiFileUpLoad(file, "", CKEDITOR_DEFAULT_PATH, 0);
				filemap.put("file_size", Long.toString(fileInfo.getFilesize()));
				filemap.put("org_filenm", fileInfo.getOrginalFilename());
				filemap.put("chg_filenm", CKEDITOR_FILE_PATH + fileInfo.getSaveFilename());
				filemap.put("message", "이미지가  업데이트 되었습니다.");
				filemap.put("result","Y");
			}else{
				filemap.put("message", "이미지 파일을 업데이트 해주세요.");
				filemap.put("file_size", "");
				filemap.put("org_filenm", "");
				filemap.put("chg_filenm", "");
				filemap.put("result","N");
			}
			
		}else{
			filemap.put("message", "이미지가  업데이트 실패.");
			filemap.put("file_size", "");
			filemap.put("org_filenm", "");
			filemap.put("chg_filenm", "");
			filemap.put("result","N");
		}
		
		request.setAttribute("filemap", filemap);
		request.setAttribute("fucNum", fucNum);
		
		return mapping.findForward("success");
	}
}
